package com.ice.base.excel;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.word.Word07Writer;
import com.ice.base.fileManager.constant.BaseFileConstant;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.text.SimpleDateFormat;

public class WordTemplate {


    //http://localhost:8081/api_p/Report/wordExport?id=1
    @ApiOperation(value = "数据上报-导出word")
    @GetMapping("/api_p/Report/wordExport")
    public void wordExport(HttpServletResponse response, String id){
        try{
         //   ReportModel model = reportService.selectById(id);
            SimpleDateFormat format  =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Word07Writer writer = new Word07Writer();
            // 添加段落（标题）居中
            writer.addText(ParagraphAlignment.CENTER,new Font("方正小标宋简体", Font.BOLD, 15), "问题报告");
        //    writer.addText(ParagraphAlignment.RIGHT,new Font("方正小标宋简体", Font.PLAIN, 10), "编号："+model.getReportNo());


   /*         writer.addText(ParagraphAlignment.LEFT,new Font("宋体", Font.PLAIN, 15), "一、任务内容");

            writer.addText(new Font("宋体", Font.PLAIN, 10), "任务名称："+model.getTitle());
            writer.addText(new Font("宋体", Font.PLAIN, 10), "问题分类："+model.getClassify());
            writer.addText(new Font("宋体", Font.PLAIN, 10), "问题属性："+model.getAttributes());
            writer.addText(new Font("宋体", Font.PLAIN, 10), "上报时间："+format.format( model.getcreateTime()));
            writer.addText(new Font("宋体", Font.PLAIN, 10), "问题地点："+model.getSite());
            writer.addText(new Font("宋体", Font.PLAIN, 10), "问题内容："+model.getContent());*/
            //空格
            writer.addText(new Font("宋体", Font.PLAIN, 8), "");

         /*   String imgs = model.getImgs();
            String[] imgArray ;
            if(StringUtils.isNotEmpty(imgs)){
                imgArray  = imgs.split(",");
                for (String img:imgArray) {
                  *//*  URL url = new URL(img);
                    URLConnection conn = url.openConnection();
                    InputStream inputStream = conn.getInputStream();
*//*
                    String winLocPath = BaseFileConstant.StaticPath.STATIC_UPLOAD.getWinLocPath();
                    // writer.addPicture(inputStream, PicType.JPEG,"我",600,800);
                    File file = new File( "E:/123.jpg");
                    writer.addPicture(file, Units.toEMU(200),Units.toEMU(200));
                    //    writer.addPicture(inputStream,PicType.JPEG,"ww",800,600);
                }
            }






            DoneResultModel resultModel = doneResultService.selectLasted(model.getId());*/

            //空格
            writer.addText(new Font("宋体", Font.PLAIN, 8), "空格2");



            response.setContentType("application/x-xwd;charset=utf-8");
            //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
            String fileName = new String("数据.docx".getBytes(), "ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            ServletOutputStream out = response.getOutputStream();
            writer.flush(out, true);
            // 关闭writer，释放内存
            writer.close();
            //此处记得关闭输出Servlet流
            IoUtil.close(out);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}
